package com.mayisoftware.mayioa.business.worktime.impl.controller;

import com.mayisoftware.mayioa.business.statistic.api.domain.EChartsBarVO;
import com.mayisoftware.mayioa.business.statistic.api.domain.EchartsPieVO;
import com.mayisoftware.mayioa.business.statistic.api.domain.SeriseData;
import com.mayisoftware.mayioa.business.worktime.api.domain.WrokTimeImportInfo;
import com.mayisoftware.mayioa.business.worktime.api.domain.board.TMBoardResult;
import com.mayisoftware.mayioa.business.worktime.api.domain.board.TMStatisticInfo;
import com.mayisoftware.mayioa.business.worktime.api.service.IWorkTimeBoardService;
import com.mayisoftware.mayioa.business.worktime.api.service.IWorkTimeService;
import com.mayisoftware.mayioa.common.api.annotation.Log;
import com.mayisoftware.mayioa.common.api.base.AjaxResult;
import com.mayisoftware.mayioa.common.api.enums.BusinessType;
import com.mayisoftware.mayioa.common.impl.config.Global;
import com.mayisoftware.mayioa.common.impl.support.StrFormatter;
import com.mayisoftware.mayioa.common.impl.utils.StringUtils;
import com.mayisoftware.mayioa.platform.api.domain.SysDictData;
import com.mayisoftware.mayioa.platform.api.domain.SysUser;
import com.mayisoftware.mayioa.platform.impl.util.FileUploadUtils;
import com.mayisoftware.mayioa.platform.impl.util.ShiroUtils;
import com.mayisoftware.mayioa.platform.impl.web.base.BaseController;
import com.mayisoftware.mayioa.platform.impl.web.page.TableDataInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.*;

@Controller
@RequestMapping("/worktime/board")
public class WorkTimeBoardController extends BaseController{
    private static final Logger log = LoggerFactory.getLogger(WorkTimeBoardController.class);

    @Autowired
    private IWorkTimeBoardService boardService;

    private String prefix = "worktime/board";

    @RequiresPermissions("worktime:board:view")
    @GetMapping()
    public String user()
    {
        return prefix + "/board";
    }


    @RequiresPermissions("worktime:board:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WrokTimeImportInfo record)
    {
        startPage();
        TMBoardResult result = boardService.queryBoardInfos(record);
        return getDataTable(result.getLess8Hours());
    }


    /**
     * 根据Bu统计饼图
     */
    @RequiresPermissions("worktime:board:view")
    @PostMapping("/list/weekendOver8Hours")
    @ResponseBody
    public EChartsBarVO weekendOver8Hours()
    {
        EChartsBarVO eChartsBarVO = new EChartsBarVO();
        List<String> data = new ArrayList<String>();
        List<SeriseData> seriesData = new ArrayList<SeriseData>();
        Set<String> xAixsData = new HashSet<String>();

//        List<TMStatisticInfo> list= statisticService.getStatisticByCheckPdu();
//
//        for(TMStatisticInfo sd: list){
//            String checkUnit = dictService.selectDictLabel("check_unit",sd.getCheckPdu());
//            if(checkUnit==null){
//                checkUnit = "未指定考核单元";
//            }
//            xAixsData.add(checkUnit);
//            checkPduFilter.add(sd.getCheckPdu());
//        }
//        eChartsBarVO.setxAixsData(xAixsData);
//        eChartsBarVO.setLegnedData(data);
//        eChartsBarVO.setSeriesData(seriesData);
//
//        List<SysDictData> recruitDicts = dictService.selectDictDataByType("recruit_status");
//
//
//        for (SysDictData dict :recruitDicts) {
//            if(dict.getDictValue().equalsIgnoreCase("0")){
//                continue;
//            }
//            SeriseData sData = new SeriseData();
//            String complete = dictService.selectDictLabel("recruit_status",dict.getDictValue());
//            sData.setName(complete);
//            sData.setType("bar");
//            sData.setStack("checkPdu");
//            List<Integer> ndata = new ArrayList<Integer>();
//            for (String checkPdu: checkPduFilter) {
//                int num =0;
//                for (HCStatisticInfo sd :list) {
//                    if ((checkPdu != null && sd.getCheckPdu() != null) &&!checkPdu.equalsIgnoreCase(sd.getCheckPdu())){
//                        continue;
//                    }
//                    if (dict.getDictValue().equalsIgnoreCase(sd.getCompleteStatus())){
//                        num = sd.getNum();
//                        break;
//                    }
//                }
//                ndata.add(num);
//            }
//            sData.setData(ndata);
//
//            seriesData.add(sData);
//        }
        return eChartsBarVO;
    }

    @RequiresPermissions("worktime:board:init")
    @PostMapping("/init")
    @ResponseBody
    public AjaxResult init()
    {
        AjaxResult ajaxResult = new AjaxResult();
//        ajaxResult.put("pdu", boardService.queryInitOption());
//        ajaxResult.put("region", boardService.queryInitOption());

        return ajaxResult;
    }

    @Log(title = "上传计划错误下载", businessType = BusinessType.EXPORT)
    @RequiresPermissions("worktime:board:download")
    @PostMapping("/download")
    @ResponseBody
    public AjaxResult downloadErrorInfo(String id)
    {
        List<WrokTimeImportInfo> list = null;//uploadRecordService.queryRecordListById(id);

        if (CollectionUtils.isEmpty(list)){
            return  error("没有错误日志信息");
        }

        String errorPath  = list.get(0).getErrorPath();
        if (StringUtils.isEmpty(errorPath)){
            return  error("没有错误日志信息");
        }

        File errorFile = new File(errorPath);

        if (!errorFile.exists() || !errorFile.isFile()){
            return  error("错误日志文件不正确.");
        }

        return success(errorPath.replace("\\","/"));
    }


    /**
     * 新增计划
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        return prefix + "/add";
    }

}
